function gwebmail_window() {
	//alert("gw::init");
	this.page_title = document.title; // creates public variable
	this.focus = true; 
	this.window_list = new Array();


	this.window = function( gwebmail_id, gwebmail_username , minimizeChatBox ) {
		//alert("gw::window(" + gwebmail_id + " , " + gwebmail_username + ")");
		chatboxtitle = gwebmail_id ;
		//alert("create chat window with " + gwebmail_id + " / " + gwebmail_username );
		if ($("#chat_"+ gwebmail_id ).length > 0) {
			if ($("#chat_" + gwebmail_id ).css('display') == 'none') {
				$("#chat_" + gwebmail_id ).css('display','block');
				//restructureChatBoxes();
				this.refresh();
			}
			$("#chat_" + gwebmail_id +" .input").focus();
			return;
		}
		$(" <div />" ).attr("id","chat_"+ gwebmail_id )
		.addClass("chatbox")
		.html(
			'<div class="chatboxhead">' + 
				'<div class="chatboxtitle"> ' + gwebmail_username + '</div>' + 
				'<div class="chatboxoptions">' + 
					'<a href="javascript:void(0)" onclick="javascript:gc.gw.close(\''+ gwebmail_id +'\')">X</a>' +
				'</div>' + 
				'<br clear="all"/>' +
			'</div>' + 
			
			'<div class="chatboxcontent"></div>' + 
			'<div class="chatboxinput">' + 
				'<textarea class="chatboxtextarea" onkeydown="javascript:return gc.gw.inputHandler(event,this,\''+ gwebmail_id +'\');"></textarea>' + 
			'</div>'
		).appendTo($( "body" ));

		$("#chat_" + gwebmail_id ).css('bottom', '0px');
		total_windows = 0;
		for (x in this.window_list) {
			if ($("#chat_"+ this.window_list[x].id).css('display') != 'none') {
				total_windows++;
			}
		}

		if (total_windows == 0) {
			$("#chat_"+ gwebmail_id ).css('right', '20px');
		} else {
			width = ( total_windows )*(225+7)+20;
			$("#chat_" + gwebmail_id ).css('right', width+'px');
		}

		this.window_list.push( {id : gwebmail_id, username: gwebmail_username } );


		// blur, focus, click
		$("#chat_" + gwebmail_id +" .chatboxtextarea").blur(function(){
			//chatboxFocus[chatboxtitle] = false;
			$("#chat_"+ gwebmail_id +" .chatboxtextarea").removeClass('chatboxtextareaselected');
		}).focus(function(){
			//chatboxFocus[chatboxtitle] = true;
			//newMessages[ gwebmail_id ] = false;
			$('#chat_'+ gwebmail_id +' .chatboxhead').removeClass('chatboxblink');
			$("#chat_"+ gwebmail_id +" .chatboxtextarea").addClass('chatboxtextareaselected');
		});

		$("#chat_"+ gwebmail_id ).click(function() {
			if ($('#chat_'+ gwebmail_id +' .chatboxcontent').css('display') != 'none') {
				$("#chat_"+ gwebmail_id +" .chatboxtextarea").focus();
			}
		});

		$("#chat_" + gwebmail_id ).show();
	}
	this.close = function( user_id ) {
		//alert("gw::close");
		$('#chat_' + user_id ).css('display','none');
		// clear the content ...
		$("#chat_" + user_id + " .chatboxcontent").html('');
		this.refresh();
	}
	this.refresh = function() {
		align = 0;

		for (x in this.window_list) {
			if ($("#chat_" + this.window_list[x].id ).css('display') != 'none') {
				if (align == 0) {
					$("#chat_" + this.window_list[x].id ).css('right', '20px');
				} else {
					width = (align)*(225+7)+20;
					$("#chat_" + this.window_list[x].id ).css('right', width+'px');
				}
				align++;
			}
		}
		//alert("gw::refresh");
	}
	this.inputHandler = function(event, target, gwebmail_id ) {
		 
		if(event.keyCode == 13 && event.shiftKey == 0)  {
			message = $(target).val();
			message = message.replace(/^\s+|\s+$/g,"");
			$(target).val('');
			$(target).focus();
			$(target).css('height','44px');
			if (message != '') {
				$.post("?ajax&module=mail&bwid=" + bwid + "&page=chat.send", {to: gwebmail_id , message: message} , function(data){
					message = message.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\"/g,"&quot;");
					$("#chat_" + gwebmail_id + " .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+ gc.me.username +':&nbsp;&nbsp;</span><span class="chatboxmessagecontent">'+message+'</span></div>');
					$("#chat_" + gwebmail_id + " .chatboxcontent").scrollTop($("#chat_" + gwebmail_id + " .chatboxcontent")[0].scrollHeight);
				});
			}
			return false;
		}
		var adjustedHeight = target.clientHeight;
		var maxHeight = 94;
		if (maxHeight > adjustedHeight) {
			adjustedHeight = Math.max(target.scrollHeight, adjustedHeight);
			if (maxHeight) adjustedHeight = Math.min(maxHeight, adjustedHeight);
			if (adjustedHeight > target.clientHeight) $(target).css('height',adjustedHeight+8 +'px');
		} else {
			$(target).css('overflow','auto');
		}
		//alert("gw::inputHandler");		 
	}

	this.onfocus = function () {
		//alert("gw::onfocus");
		this.focus = true;
		document.title = this.page_title;
	}
	this.onblur = function () {
		//alert("gw::onblur");
		this.focus = false;
	}
	
	this.functionname = function (parameter) {
	}
}
function gwebmail_chat() {
	//alert("gc::init");
	this.me = {};
	this.gw = new gwebmail_window();

	$.ajax({
	  url: "?ajax&module=mail&bwid=" + bwid + "&page=chat.init", cache: false, dataType: "json", success: function(data) {
		gc.me.user_id = data.me_id;
		gc.me.username = data.me_username;
		gc.me.name = data.me_name;
		gc.me.nickname = data.me_nickname;
		gc.me.domain = data.me_domain;

		//alert("I AM " + gc.me.username );

		$.each(data.items, function(i,item){
			
			if (item)	{ 
				//alert("chat::item -> " + item.f);
				if ($("#chat_" +  item.f ).length <= 0) {
					gc.gw.window(item.f,item.f,1);
				}
				if (item.s == 1) {
					//item.f = username;
					item.f = gc.me.user_id;
				}
				if (item.s == 2) {
					$("#chat_" + item.f + " .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxinfo">' + item.m + '</span></div>');
				} else {
					$("#chat_" + item.f + " .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">' + item.f + ':&nbsp;&nbsp;</span><span class="chatboxmessagecontent">' + item.m + '</span></div>');
				}
			}

		});


		for (i=0;i < gc.gw.window_list.length;i++) {
			//chatboxtitle = chatBoxes[i];
			$("#chat_" + gc.gw.window_list[i].user_id + " .chatboxcontent").scrollTop($("#chat_" + gc.gw.window_list[i].user_id +" .chatboxcontent")[0].scrollHeight);
			setTimeout('$("#chat_" + gc.gw.window_list[i].user_id + " .chatboxcontent").scrollTop($("#chat_" + gc.gw.window_list[i].user_id + " .chatboxcontent")[0].scrollHeight);', 100); // yet another strange ie bug
		}
		setTimeout('gc.handler();',3000);
	}});


	this.talkto = function ( gwebmail_id, gwebmail_username ) {
		//alert("gc::talkto(" + user_id + ", " + user_name + ")");
		this.gw.window(  gwebmail_id, gwebmail_username );	
		$("#chat_" + gwebmail_id + " .chatboxtextarea").focus();
	}
	this.handler = function () {
		$.ajax({
		  url: "?ajax&module=mail&bwid=" + bwid + "&page=chat.get",
		  cache: false,
		  dataType: "json",
		  success: function(data) {

			$.each(data.items, function(i,item){
				if (item) {
					//chatboxtitle = item.f;

					if ($("#chat_" + item.f ).length <= 0) {
						gc.gw.window(item.window,item.username);
					}
					if ($("#chat_" + item.f ).css('display') == 'none') {
						$("#chat_" + item.f ).css('display','block');
						gc.gw.refresh();
					}

					if (item.s == 1) {
						item.f = gc.me.user_id;
					}
					if (item.s == 2) {
						$("#chat_" + item.window + " .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxinfo">'+item.m+'</span></div>');
					} else {
						$("#chat_" + item.window + " .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+item.username+':&nbsp;&nbsp;</span><span class="chatboxmessagecontent">'+item.m+'</span></div>');
					}
					$("#chat_" + item.window + " .chatboxcontent").scrollTop($("#chat_" + item.window + " .chatboxcontent")[0].scrollHeight);
				}
			});
			setTimeout('gc.handler();',5000);
		}});
	}
}

$(document).ready(function(){
	gc = new gwebmail_chat();
	$([window, document]).blur(function(){
		gc.gw.onblur();
	}).focus(function(){
		gc.gw.onfocus();
	});

});